

define(function(require,exports,module){
	
	var leftWidth = 220,foldWidth = 26;//折叠后宽度 

	//生成header
	function createHeader(mainBody,userData){
		var self = this;
		return [{
			eName:"div",
			cssClass:"titlebar",
			elements:{
				eName:"span",
				text:'烟台市公安消防灭火救援移动指挥辅助决策平台--指挥中心管理系统',
				onClick:function(){
					$ui.print("hello");
					//$ui.postEx("fire/firecaseinfo/hisjsons.do",{kssj:'2017-07-14',jssj:'2017-07-15'});
					//$ui.postEx("fire/firecaseinfo/updatepos.do",{caseId:1,zhdz:'xxxx',gpsLng:120.123,gpsLat:37.89});
					//$ui.postEx("sys/fireusergps/save.do",{lon:10,lat:37.89,alt:1,speed:2,diretion:23});
					$ui.postEx("sys/fireusergps/save.do",{lon:121.350843,lat:37.498656,alt:0,speed:0,direction:'0.0'});
				}
			}
		},{
			eName:"div",
			cssClass:"title-button",
			elements:[{
				eName:"linkbutton",
				plain:true,
				iconCls: 'icon-Modify_Pwd',
				text:"修改密码",
		        onClick:function(){
		        	mainBody.loadJs("js/sys/editPwd.js",null,true);
		        }
		   },{
			   eName:"linkbutton",
			   plain:true,
			   width:60,
			   iconCls: 'icon-Login_out',
			   text:"退出",
		       onClick:function(){
	        		 $ui.postEx("logout.do",null,function(){
	        			 mainBody.loadJs("js/boot/login");
	        		 });
		       }
		   }]
		}];
	}
	//创建界面
	function createUI(sysData){
		var tabsDom = null;
		var mainBody = this.jqDom;
		var self = this;
		var user = sysData.user;
		var ui = {
			eName:"layoutExt",
			addMode:'html',
			cssClass:"index_div_layout",
			onResize:function(){
				if(self.tabsDom) self.tabsDom.tabs('resize');
			},
			elements:[{
				region:"north",
				height:50,
				cssClass:"div_layout_top",
				elements:createHeader.call(self,mainBody,user)//header
			},{
				region:"center",
				elements:{
		    	    eName:'tabs',
		    	    fit:true,
		    	    elements:[{
		    	    	title:'首页',
		    	    	iconCls: 'icon-hamburg-home'
		    	    }]
		    	}
			},{
				region:"west",
				width:leftWidth,
				cssStyle:"position:relative;",
				elements:[{//菜单面板
				     eName:"div",
				     cssClass:"div_layout_left",
				     id:"layout_left_on",
				     top:0,
				     left:0,
				     right:0,
				     elements:[{//标题头
				    	eName:"div",
				    	cssClass:"panel-header",
			    		elements:[{
			    			eName:"div",
			    			cssClass:"panel-title",
			    			text:"欢迎光临," + user.userName
			    		},{
			    			eName:"div",
			    			cssClass:"panel-tool",
			    			elements:{
			    				eName:'a',
			    				cssClass:"layout-button-left",
				    			onClick:function(){//折叠菜单按钮
				    				mylayout.layoutExt("setPanelSize",
				    						{region:"west",index:0,width:foldWidth});
				    				layoutLeft.hide();
				    				myLayoutLeftExpand.show();
				    				self.tabsDom.tabs('resize');
				    				return false;
				    		   }
			    		}
			    	    }]
				     },{//菜单正文
			    		eName:"div",
			    		cssClass:"panel-menu-body",
			    		id:"div_left_accordion",
			    		elements:creatMenus.call(self,sysData.menuList)//菜单列表,
			    	}]
			    },{
			    	eName:"div",
			    	cssClass:"div_layout_left",
			    	cssStyle:"display: none;background-color:#E0ECFF",
			    	id:"layout_left_off",
		    		width:foldWidth,
		    		top:0,
		    		elements:[{
		    			eName:"div",
		    			cssClass:"panel-header",
		    			cssStyle:"border:none;",
		    			elements:[{
		    				eName:"div",
		    				cssClass:"panel-title"
		    			},{
		    				eName:"div",
		    				cssClass:"panel-tool",
		    				elements:{
		    					eName:'a',
		    					cssClass:"layout-button-right",
				    			onClick:function(){
					    			layoutLeft.show();
							    	myLayoutLeftExpand.hide();
					    			mylayout.layoutExt("setPanelSize",
					    					{region:"west",index:0,width:leftWidth});
					    			self.tabsDom.tabs('resize');
				    			}
				    		}
		    			}]
		    		}]
			    }]
			}]
		}
		mainBody.createUI(ui);
		var mylayout = mainBody.findJq("layoutExt");
		var layoutLeft = mainBody.find("#layout_left_on");
		var myLayoutLeftExpand = mainBody.find("#layout_left_off");
		this.tabsDom = mainBody.findJq("tabs");
		this.tabsDom.tabs('resize');
	}
	
	function Main(jqDom,loadParams){
		this.jqDom = jqDom;
		var retData = $ui.loadEx("indexJson.do");//获取系统全局数据
		if(retData.result == false) return;
		createUI.call(this,retData.data);
	}
	//生成菜单集合
  function creatMenus(menuList){
		var self = this;
		//标题点击事件
		function headerClick(){
			var jqSelected = $('.accordion-header-selected');
			//关闭上一个
			if(jqSelected[0] != $(this).get(0)){
				jqSelected.next(".panel-body").toggle();
				jqSelected.removeClass("accordion-header-selected");
				jqSelected.find('.accordion-collapse').toggleClass("accordion-expand");
			}
			$(this).toggleClass('accordion-header-selected');
			$(this).next(".panel-body").toggle();
			$(this).find('.accordion-collapse').toggleClass("accordion-expand");
		}
		//菜单点击事件
		function menuClick(){
			var jqObj = $(this);
			var m = jqObj.findOpts();
			var name = $.trim(jqObj.text());
			createTab(self.tabsDom,{'title':name,'url':m.url});
		}
		var flag = true;
		return $.map(menuList,function(menu1,i){
			if(menu1.parentId != 0) return null;//不是根目录
			var subMenus = $.map(menuList,//生成子菜单
					function(menu2,j){
						if(menu2.parentId != menu1.menuId) return null;
						return {
							eName:'a',
							cssClass:"layout_left_menu",
							url:menu2.url,
			    			onClick:menuClick,/*菜单点击事件*/
			    			elements:{
			    				eName:"span",
			    				text:menu2.name
			    			}
				    	};
				    }
			);
			var menu = {
				eName:'div',
				cssClass:'accordion accordion-noborder',
				cssStyle:"text-align: left;",
				elements:[{
					eName:'div',
					cssClass:'panel-header panel-header-noborder accordion-header ' 
								+ (flag?" accordion-header-selected":""),
					height:16,
					onClick:headerClick,
					elements:[{
						eName:'div',cssClass:'panel-title panel-with-icon',
						text:menu1.name				
					},{
						eName:'div',
						cssClass:'panel-tool',
						elements:{
							eName:'a',
							cssClass:"accordion-collapse " 
										+ (flag?"":"accordion-expand")
					    }
					}]
				},{
					eName:'div',
					cssClass:'panel-body panel-body-noborder accordion-body',
					cssStyle:"overflow: hidden;  height: auto;display:" 
								+ (flag?"block;":"none;"),
					elements:subMenus
				}]
			};
			if(flag) flag=false;
			return menu;
		});
	}
	//创建tabs
	function createTab(tabObj,params){
		var url = params.url;
		if(url == false) return;
		if($ui.isJsUrl(url) == false){//
			params.href = url;
		}
		params = $.extend({
	   	    closable:true,
	   	    bodyCls:'dialog_panelbody',
	   	    method:"post",
	   	    onLoadError:function(){
	   	    	$ui.alert('404 !');
	   	    },tools:[{
	   	    	iconCls:'icon-mini-refresh',
	   	        handler:function(){
	   	        	var myTabs = tabObj;
	   	        	var pp = myTabs.tabs('getSelected');
	   	        	var ppopt = pp.panel('options');
	   	        	var litab = $(this).parent().parent();
	   	        	var title = litab.find('.tabs-title').text();
	   	        	//切换tab时，点击了刷新按钮，则不刷新tab
	   	        	if(title != ppopt.title) return;
	   	        	if($ui.isJsUrl(url)){
	   	        		pp.empty();
	   	        		pp.loadJs(url,params.queryParams);
		   			}else  pp.panel('refresh',ppopt.href);
	   	        }
	   	    }]
	   	},params);
		if (tabObj.tabs('exists', params.title)) {//存在选择
			tabObj.tabs('select', params.title);
		}else{//添加
			tabObj.tabs('add', params);
			if($ui.isJsUrl(url)){//加载js
				var pp = tabObj.tabs("getTab",params.title);
				pp.empty();
	        	pp.loadJs(url,params.queryParams);
   			}
		}
	}
	
	module.exports = Main;
	
});
	